<template>
  <div class="login">
    <div class="login-back">
      <img src="../../assets/images/login-back.png" alt="" />
    </div>

    <!-- 登录面板 -->
    <div class="login-box" @keyup.enter="submitForm">
      <div class="login-box-title">副业帮帮管理系统</div>
      <div class="login-box-from">
        <el-form
          :model="loginForm"
          :rules="rules"
          ref="loginForm"
          class="demo-ruleForm"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              size="medium"
            >
              <el-button slot="prepend" icon="el-icon-user" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :type="!pwdShow ? 'password' : 'text'"
              v-model="loginForm.password"
              placeholder="请输入密码"
              size="medium"
            >
              <i
                slot="prepend"
                @click="pwdShow = !pwdShow"
                class="el-icon-key"
              />
            </el-input>
          </el-form-item>
          <!-- <el-form-item prop="code">
            <el-input
              type="text"
              clearable
              v-model.trim="loginForm.code"
              placeholder="验证码"
            >
              <template slot="append">
                <div
                  class="login-code"
                  v-html="loginForm.codeSvg"
                  @click="getCodeSvg"
                ></div>
              </template>
            </el-input>
          </el-form-item> -->
          <el-form-item style="margin-top: 40px">
            <el-button
              type="primary"
              size="medium"
              :loading="loading"
              style="width: 100%"
              @click.native="submitForm"
              >登陆</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from "../../api/index";

import { md5 } from "../../utils/md5";

export default {
  data() {
    return {
      loading: false, //登陆状态
      pwdShow: false,
      loginForm: {
        // 登陆表单
        username: "",
        password: "",
        // code: "1111111111",
        codeSvg: "",
        key: "",
      },
      rules: {
        //登陆验证规则
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 2,
            max: 18,
            message: "长度在 2 到 18 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
        // code: [
        //   { required: true, message: "请输入验证码", trigger: "blur" },
        //   {
        //     min: 6,
        //     max: 16,
        //     message: "长度在 6 到 16 个字符",
        //     trigger: "blur",
        //   },
        // ],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.loginForm.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        this._login();
      });
    },
    getCodeSvg() {
      showLoading("正在获取验证码");
    },
    _login() {
      let params = {
        managerName: this.loginForm.username,
        managerPassword: md5(this.loginForm.password),
      };

      login(params).then((res) => {
        if (res.errorCode == null) {
          let token = res.resultData.accessToken;
          // //     本地  vuex
          this.$store.commit("LOGIN_IN", token);

          localStorage.setItem("info", JSON.stringify( res.resultData));
          localStorage.setItem("accessToken", token);

          this.$router.push("/");

          this.loading = false
        } else {
           this.loading = false
        }
      });
    },
  },
};
</script>


<style lang="less" scoped>
.login {
  height: 100vh;
  width: 100vw;
  position: relative;

  .login-back {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    display: flex;

    img {
      width: 100%;
      height: 100%;
    }
  }

  .login-box {
    position: relative;
    width: 450px;
    background: #fff;
    border-radius: 5px;
    box-shadow: 0 0 2px #fff;
    border: 1px solid #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .login-box-title {
      line-height: 90px;
      font-size: 18px;
      color: #1890ff;
      font-weight: bold;
      display: flex;
      justify-content: center;
      align-items: center;
      border-bottom: 1px solid #ffffff;
    }

    .login-box-from {
      width: 100%;
      height: auto;
      padding: 0 30px 30px;
      box-sizing: border-box;

      .el-icon-key {
        cursor: pointer;
      }

      .login-code {
        cursor: pointer;
        height: 44px;
      }

      /deep/ .el-input-group {
        height: 44px;
      }

      /deep/ .el-input__inner {
        height: 44px;
      }

      /deep/ .el-button--medium {
        padding: 15px 20px;
        font-size: 16px;
        cursor: pointer;
      }
    }
  }
}
</style>